package com.bootdo.market.controller;

import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.market.domain.AccountDO;
import com.bootdo.market.service.AccountService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author jinse
 * @email market@jinse.com
 * @date 2019-03-28 18:09:01
 */

@Controller
@RequestMapping("/market/account")
public class AccountController {
    @Autowired
    private AccountService accountService;
    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @GetMapping()
    @RequiresPermissions("market:account:account")
    String Account() {
        return "market/account/account";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("market:account:account")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<AccountDO> accountList = accountService.list(query);
        int total = accountService.count(query);
        PageUtils pageUtils = new PageUtils(accountList, total);
        return pageUtils;
    }

    @GetMapping("/add")
    @RequiresPermissions("market:account:add")
    String add() {
        return "market/account/add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("market:account:edit")
    String edit(@PathVariable("id") Integer id, Model model) {
        AccountDO account = accountService.get(id);
        if (account == null) {
            account = new AccountDO();
        }
        account.setChannelid(id.toString());
        model.addAttribute("account", account);
        return "market/account/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("market:account:add")
    public R save(AccountDO account) {
        account.setPassword(bCryptPasswordEncoder.encode("00000000"));
        Integer id = null;
        if (account.getChannelid() != null) {
            id = Integer.valueOf(account.getChannelid());
        }
        AccountDO accountDo = accountService.get(id);
        if (accountDo == null) {
            if (accountService.save(account) > 0) {
                return R.ok();
            }
        } else {
            if (accountService.update(account) > 0) {
                return R.ok();
            }
        }
        return R.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/resetPasswd")
    @RequiresPermissions("market:account:resetPasswd")
    public R resetPasswd(AccountDO account) {
        AccountDO accountDo = new AccountDO();
        accountDo.setChannelid(account.getChannelid());
        accountDo.setPassword(bCryptPasswordEncoder.encode("00000000"));
        accountDo.setId(account.getId());
        if (accountService.update(accountDo) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("market:account:remove")
    public R remove(Integer id) {
        if (accountService.remove(id) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("market:account:batchRemove")
    public R remove(@RequestParam("ids[]") Integer[] ids) {
        accountService.batchRemove(ids);
        return R.ok();
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/findByUserName")
    @RequiresPermissions("market:account:findByUserName")
    public int findByUserName(AccountDO account) {
        AccountDO aa = accountService.findByUserName(account.getUsername());
        if (aa == null) {
            return 0;
        } else {
            return 1;
        }

    }


}
